﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Windows;
using System.Windows.Media.Imaging;

namespace Trade.Journal
{
    /// <summary>
    /// AddPicWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddPicWindow : Window
    {
        public AddPicWindow()
        {
            InitializeComponent();
        }

        private void ScreenShot_Click(object sender, RoutedEventArgs e)
        {
            foreach (Window item in Application.Current.Windows)
            {
                item.WindowState = WindowState.Minimized;
            }

            if (DataContext is AddPicWindowVM v)
                v.SelectedImage = ScreenDevice.GetDesktopImage();

            foreach (Window item in Application.Current.Windows)
            {
                item.WindowState = WindowState.Normal;
            }

            CenterOwner();
        }

        private void GetFromClipboard_Click(object sender, RoutedEventArgs e)
        {
            if (DataContext is AddPicWindowVM v)
            {
                v.SelectedImage = Clipboard.GetImage();
                if (v.SelectedImage != null)
                    CenterOwner();
            }
        }


        /// <summary>
        /// 在主窗口中居中显示
        /// </summary>
        /// <param name="subWindow"></param>
        /// <param name="parentWindow"></param>
        private void CenterOwner()
        {
            Window parentWindow = Owner;
            if (parentWindow == null)
                return;

            Width = parentWindow.Width;
            //Height = parentWindow.Height;
            //窗口居中显示
            Left = parentWindow.Left + (parentWindow.ActualWidth - ActualWidth) / 2;
            Top = parentWindow.Top + (parentWindow.ActualHeight - ActualHeight) / 2;
        }



        private void ConfirmedCommand_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            if (DataContext is AddPicWindowVM v && v.SelectedBarType != null && v.SelectedImage != null)
                e.CanExecute = true;
            else
                e.CanExecute = false;
        }

        private void ConfirmedCommand_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            //if (DataContext is AddPicWindowVM v)
            //    DataContext = v.SelectedImage;

            DialogResult = true;
            Close();
        }
    }


    public class AddPicWindowVM : BindableBase
    {
        public AddPicWindowVM()
        {
            var sec = ConfigurationManager.GetSection("BarTypes") as List<BarCycle>;
            BarTypes = new ObservableCollection<BarCycle>(sec);
        }

        public ObservableCollection<BarCycle> BarTypes { get; }


        private BarCycle? _SelectedBarType;

        public BarCycle? SelectedBarType
        {
            get { return _SelectedBarType; }
            set { SetProperty(ref _SelectedBarType, value); }
        }

        private BitmapSource _SelectedImage;

        public BitmapSource SelectedImage
        {
            get { return _SelectedImage; }
            set { SetProperty(ref _SelectedImage, value); }
        }


    }
}
